package com.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.entity.Phone;
import com.shop.entity.Res;
import com.shop.entity.User;
import com.shop.entity.UserOrder;
import com.shop.service.PhoneService;
import com.shop.service.UserOrderService;
import com.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    PhoneService phoneService;
    @Autowired
    UserOrderService userOrderService;

    @PostMapping("login")
    public Res login(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());
        User one = userService.getOne(queryWrapper);
        if (one == null) {
            return Res.error("没有这个用户");
        }
        if(!one.getPassword().equals(user.getPassword())){
            return Res.error("密码错误");
        }
        return Res.success(one);
    }

    @PostMapping("reg")
    public Res reg(@RequestBody User user) {
        User one = userService.getOne(new QueryWrapper<User>().eq("user_name", user.getUserName()));
        if (one != null){
            return Res.error("用户已存在");
        }
        user.setCreateTime(new Date());
        user.setAccount(0F);
        boolean save = userService.save(user);
        return save? Res.success(user): Res.error();
    }
    @PostMapping("adminLogin")
    public Res adminLogin(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());
        User one = userService.getOne(queryWrapper);

        if (one == null || one.getStatus().equals(UserService.NOT_ADMIN)) {
            return Res.error("没有这个用户");
        }

        if(!one.getPassword().equals(user.getPassword())){
            return Res.error("密码错误");
        }
        return Res.success(one);
    }

    @GetMapping("count")
    public Res count () {
        return Res.success(userService.count());
    }

    @GetMapping("listByPage")
    public Res listPage ( Integer start,Integer offset){
        List<User> list = userService.list();
        List<User> users = list.subList(start >= list.size() ? list.size()  : start, Math.min(start + offset, list.size()));
        return Res.success(users);
    }

    @PostMapping("charge")
    public Res charge(@RequestBody HashMap<String,String> params) {
        String userName = params.get("userName");
        Float money = Float.parseFloat(params.get("money").toString());
        User one = userService.getOne(new QueryWrapper<User>().eq("user_name", userName));
        one.setAccount(one.getAccount() + money);
        boolean update = userService.update(one, new QueryWrapper<User>().eq("user_name", userName));
        return update? Res.success(): Res.error();
    }

    @PostMapping("purchase")
    public Res purchase(@RequestBody HashMap<String,String> params) {
        String userName = params.get("userName");
        String phoneId = params.get("phoneId");
        Integer num = Integer.parseInt(params.get("num").toString());
        Phone phone = phoneService.getById(phoneId);
        User user = userService.getOne(new QueryWrapper<User>().eq("user_name", userName));
        if(user.getAccount() < num * phone.getPrice()){
            return Res.error("钱不够");
        }
        user.setAccount(user.getAccount() - phone.getPrice());
        boolean update = userService.update(user, new QueryWrapper<User>().eq("user_name", userName));
        for (int i = 0; i < num; i++) {
            UserOrder userOrder = new UserOrder();
            userOrder.setStatus(UserOrderService.NOT_COMMENT);
            userOrder.setId(UUID.randomUUID().toString());
            userOrder.setPhoneId(phoneId);
            userOrder.setUserName(userName);
            userOrder.setMemory(phone.getMemory().toString());
            userOrder.setCreateTime(new Date());
            userOrder.setPhoneName(phone.getPhoneName());
            userOrder.setDisk(phone.getDisk().toString());
            userOrder.setPrice(phone.getPrice());
            userOrder.setLogistics(UserOrderService.NOT_SEND);
            userOrder.setAddress(user.getAddress());
            userOrder.setCoverSrc(phone.getCoverSrc());
            userOrderService.save(userOrder);
        }

        return update  ? Res.success() : Res.error();
    }

    @GetMapping("getUser")
    public Res getUser(String userName) {
        User one = userService.getOne(new QueryWrapper<User>().eq("user_name",userName));
        return one != null ? Res.success(one) : Res.error();
    }

    @PostMapping("changeAttr")
    public Res changeAttr(@RequestBody User user) {

        boolean update = userService.update(user, new QueryWrapper<User>().eq("user_name", user.getUserName()));
        User one = userService.getOne(new QueryWrapper<User>().eq("user_name",user.getUserName()));

        return update?Res.success(one):Res.error();
    }

    @PostMapping("resetPasswd")
    public Res resetPasswd(@RequestBody HashMap<String,String> params) {
        String odlPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");
        String userName = params.get("userName");
        User one = userService.getOne(new QueryWrapper<User>().eq("user_name", userName));

        if(!one.getPassword().equals(odlPassword)){
            return Res.error("密码错误");
        }
        one.setPassword(newPassword);
        boolean update = userService.update(one, new QueryWrapper<User>().eq("user_name", userName));
        return update?Res.success():Res.error();

    }
}

